package com.barneyx.mshinfoplatform.common;

import cn.hutool.core.date.DateUtil;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class ExResult<T> implements Serializable {
    private String msg;
    @Builder.Default
    private boolean state = false;
    private int code;
    private T data;
    private Date date;
    private Long time;


    public ExResult(String msg, T data) {
        this.state = true;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ExResult<T> ofSuccess(T data, ResponseCode code) {
        return ExResult.<T>builder()
                .data(data)
                .msg(code.getMessage())
                .code(ResponseCode.SUCCESS.getCode())
                .state(true)
                .time(System.currentTimeMillis())
                .date(DateUtil.date())
                .build();
    }

    public static <T> ExResult<T> ofSuccess(T data) {
        return ExResult.<T>builder()
                .data(data)
                .msg(ResponseCode.SUCCESS.getMessage())
                .state(true)
                .code(ResponseCode.SUCCESS.getCode())
                .date(DateUtil.date())
                .build();
    }

    public static <T> ExResult<T> ofFail(ResponseCode code) {
        return ExResult.<T>builder()
                .data(null)
                .msg(code.getMessage())
                .state(false)
                .code(code.getCode())
                .date(DateUtil.date())
                .build();
    }

    public static <T> ExResult<T> ofFail(String errmsg) {
        return ExResult.<T>builder()
                .data(null)
                .msg(errmsg)
                .state(false)
                .code(ResponseCode.FAILURE.getCode())
                .date(DateUtil.date())
                .build();
    }

    public static <T> ExResult<T> ofFail(ResponseCode code, T data) {
        return ExResult.<T>builder()
                .data(null)
                .msg(code.getMessage())
                .data(data)
                .state(false)
                .code(ResponseCode.FAILURE.getCode())
                .date(DateUtil.date())
                .build();
    }

    public static <T> ExResult<T> ofAuto(boolean flag, T data) {
        ResponseCode _code = flag ? ResponseCode.SUCCESS : ResponseCode.FAILURE;
        return ExResult.<T>builder()
                .state(flag)
                .data(data)
                .code(_code.getCode())
                .msg(_code.getMessage())
                .date(DateUtil.date())
                .build();
    }

    public static <T> ExResult<T> ofAuto(T data) {
        ResponseCode _code = data != null ? ResponseCode.SUCCESS : ResponseCode.FAILURE;
        return ExResult.<T>builder()
                .state(data != null)
                .data(data)
                .code(_code.getCode())
                .msg(_code.getMessage())
                .date(DateUtil.date())
                .build();
    }

    public static <T> ExResult<T> ofAuto(boolean flag, String succ_msg, String fail_msg, T data) {
        ResponseCode _code = flag ? ResponseCode.SUCCESS : ResponseCode.FAILURE;
        return ExResult.<T>builder()
                .state(flag)
                .data(data)
                .code(_code.getCode())
                .msg(flag ? succ_msg : fail_msg)
                .date(DateUtil.date())
                .build();
    }

    public static <T> ExResult<T> ofAuto(boolean flag, ResponseCode succ_code, ResponseCode faild_code, T data) {
        ResponseCode _code = flag ? succ_code : faild_code;
        return ExResult.<T>builder()
                .state(flag)
                .data(data)
                .msg(_code.getMessage())
                .code(_code.getCode())
                .date(DateUtil.date())
                .build();
    }


    @Getter
    public enum ResponseCode {
        SUCCESS(200, "操作成功"),
        FAILURE(201, "操作失败"),
        ERROR(500, "统异常，请稍后重试"),
        FIELD_ERROR(500, "字段异常,请检查"),

        LOGIN_SUCCESS(600, "登陆成功"),
        LOGIN_FAILD(601, "登陆失败"),

        INVALID_TOKEN(2001, "访问令牌不合法"),
        ACCESS_DENIED(2002, "没有权限访问该资源"),


        USERNAME_OR_PASSWORD_ERROR(2003, "用户名或密码错误"),

        ACCESS_USER_LOCK(2004, "当前用户不可使用!"),
        USER_BIND_CODE_OR_PHONE_ERROR(2005, "您的手机号或是验证码有误,请检查后重新填写提交!"),
        USER_REGISTER_ERROR(2006, "用户注册失败,请关闭应用后重试!"),
        USER_UPDATE_PLATFORM_INFO_ERROR(2007, "修改平台信息失败！请稍后重试，如果还未解决，联系平台管理员解决！"),

        SMS_OFFICE_ERROR(2008, "您操作的太频繁了，请稍后重试"),
        USER_WECHAT_MP_LOGIN_ERROR(2009, "微信登陆失败,请重新登陆!"),
        USER_LOGIN_ERROR(2010, "登陆失败,请重新登陆!"),
        USER_NOT_LOGIN_ERROR(2011, "您没有登陆,请登陆!"),
        USER_NOT_AUTHENTICATION_ERROR(2012, "请实名认证后,再进行操作!"),
        USER_TOKEN_ERROR(2013, "用户令牌异常，请退出应用重新登录"),
        USER_UPDATE_REAL_NAME_INFO_ERROR(2014, "修改实名信息失败！请稍后重试，如果还未解决，联系平台管理员解决！"),
        USER_INFO_NOT_FOUND_ERROR(2015, "没有找到此用户的注册信息！"),


        //info 异常
        INFO_NOT_CREATE_ERROR(5000, "信息创建失败,请稍后尝试发布"),
        INFO_NOT_FOUND_ERROR(5001, "没有查询到此条信息"),
        INFO_ID_NOT_NULL(5002, "信息编号不能为空"),


        //Cate 异常
        INFO_CATE_FIELD_ERROR(6001, "请检查关键字段是否填写全"),


        AREA_NULL(6000, "请选择地区"),
        ;


        private final int code;
        private final String message;

        ResponseCode(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }


}